package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.constant.SensorType;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.SensorEntity;
import com.whut.monitor.service.ISensorService;
import com.whut.monitor.vo.SensorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author Sandeepin
 * 传感器服务层实现
 */
@Service
public  class SensorServiceImpl implements ISensorService {

	
	@Autowired
	private IBaseDao<SensorEntity> sensorDao;
	


	@Override
	public void add(SensorVo sensorVo) {
		SensorEntity entity = new SensorEntity();
		entity.setSensorType(sensorVo.getSensorType());
		entity.setSensorNum(sensorVo.getSensorNum());
		entity.setSensorName(sensorVo.getSensorName());
		entity.setCompany(sensorVo.getCompany());
		entity.setProductDate(sensorVo.getProductDate());
		sensorDao.save(entity);
	}

	@Override
	public void delete(Integer id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		sensorDao.executeHql("delete from SensorEntity entity where entity.id=:id",params);
	}

	@Override
	public void edit(SensorVo sensorVo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sensorName", sensorVo.getSensorName());
		params.put("sensorNum", sensorVo.getSensorNum());
		params.put("sensorType", sensorVo.getSensorType());
		params.put("productDate", sensorVo.getProductDate());
		params.put("company", sensorVo.getCompany());
		params.put("id", sensorVo.getId());
		String hql="update SensorEntity entity set entity.sensorName=:sensorName,entity.sensorNum=:sensorNum,entity.sensorType=:sensorType"
				+ ",entity.productDate=:productDate,entity.company=:company where entity.id=:id";
		sensorDao.executeHql(hql, params);
	}

	
	@Override
	public List<SensorVo> dataGrid(SensorVo sensorVo, PageFilter pageFilter){
		List<SensorVo> ul = new ArrayList<SensorVo>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from SensorEntity entity ";
		List<SensorEntity> list = sensorDao.findList(hql + whereHql(sensorVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
		for (SensorEntity entity : list) {
			SensorVo u = new SensorVo();
			BeanUtils.copyProperties(entity, u);

			if(SensorType.ELETRONIC.getType()==entity.getSensorType()){
				u.setTypeName(SensorType.ELETRONIC.getName());
			}else if (SensorType.TEMPERATURE.getType()==entity.getSensorType()) {
				u.setTypeName(SensorType.TEMPERATURE.getName());
			}else if (SensorType.STRAIN.getType()==entity.getSensorType()) {
				u.setTypeName(SensorType.STRAIN.getName());
			}else if (SensorType.CCDSFT.getType()==entity.getSensorType()) {
				u.setTypeName(SensorType.CCDSFT.getName());
			}else if (SensorType.ENSET.getType()==entity.getSensorType()) {
				u.setTypeName(SensorType.ENSET.getName());
			}
			ul.add(u);
		}
		return ul;
	}

	@Override
	public Long count(SensorVo sensorVo, PageFilter pageFilter) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from SensorEntity entity ";
		return sensorDao.count("select count(*) " + hql + whereHql(sensorVo, params), params);
	}
	
	private String whereHql(SensorVo sensorVo, Map<String, Object> params) {
		String hql = "";
		if (sensorVo != null) {
			hql += " where 1=1 ";
			if (sensorVo.getSensorName() != null) {
				hql += " and entity.name like :name";
				params.put("name", "%%" + sensorVo.getSensorName() + "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter pageFilter) {
		String orderString = "";
		if ((pageFilter.getSort() != null) && (pageFilter.getOrder() != null)) {
			orderString = " order by entity." + pageFilter.getSort() + " " + pageFilter.getOrder();
		}
		return orderString;
	}

	@Override
	public List<Tree> tree() {
		List<SensorEntity> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = sensorDao.findList("select distinct entity from SensorEntity entity");

		if ((l != null) && (l.size() > 0)) {
			for (SensorEntity r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<SensorEntity> getAllSensor() {
		List<SensorEntity> list=sensorDao.findList("from SensorEntity");
		return list;
	}

	
}
